home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 5 / Apprentice-Release5.iso / Source Code / C / Applications / RandomDot 1.1.0 / source / RandomDotScroll.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-10-21  |  5.3 KB  |  235 lines  |  [TEXT/KAHL]

  1. /* RandomDotScroll.c
  2.     by David Phillip Oster October 1994 oster@netcom.com
  3.     for:
  4.     Stuart Inglis singlis@waikato.ac.nz
  5.     Department of Computer Science
  6.     University of Waikato, Hamilton, New Zealand
  7.  */
  8. #include "RandomDotMain.h"
  9. #include "RandomDotRes.h"
  10.  
  11. #include "RandomDotWin.h"
  12. #include "Utils.h"
  13. #include "RandomDotScroll.h"
  14.  
  15. static void ScrollDraw(Integer x, Integer y);
  16. static void    DoScrollDraw(Integer, Integer);
  17.  
  18. static void    HDThumb(ControlHandle, Integer);
  19. static pascal void HThumb(void);
  20. static void    InitHThumb(ControlHandle);
  21. static void    InitVThumb(ControlHandle);
  22. static pascal void PageDown(ControlHandle, Integer);
  23. static pascal void PageLeft(ControlHandle, Integer);
  24. static pascal void PageRight(ControlHandle, Integer);
  25. static pascal void PageUp(ControlHandle, Integer);
  26. static Integer PageWidth(void);
  27. static pascal void ScrollDown(ControlHandle, Integer);
  28. static pascal void ScrollLeft(ControlHandle, Integer);
  29. static pascal void ScrollRight(ControlHandle, Integer);
  30. static pascal void ScrollUp(ControlHandle, Integer);
  31. static void VDThumb(ControlHandle, Integer);
  32. static pascal void VThumb(void);
  33.  
  34. ScrollBarClassRec VProcs = {
  35.     ScrollUp,    /* inUpButton */
  36.     ScrollDown,    /* inDownButton */
  37.     PageUp,        /* inPageUp */
  38.     PageDown,    /* inPageDown */
  39.     VThumb,        /* inThumb */
  40.     InitVThumb,    /* InitThumb */
  41.     VDThumb        /* DThumb */
  42. };
  43.  
  44. ScrollBarClassRec HProcs = {
  45.     ScrollLeft,    /* inUpButton */
  46.     ScrollRight,/* inDownButton */
  47.     PageLeft,    /* inPageUp */
  48.     PageRight,    /* inPageDown */
  49.     HThumb,        /* inThumb */
  50.     InitHThumb,    /* InitThumb */
  51.     HDThumb        /* DThumb */
  52. };
  53.  
  54.  
  55. /* ScrollUp - control proc calls us
  56.  */
  57. static pascal void ScrollUp(ControlHandle c, Integer p){
  58.     if(inUpButton == p){
  59.         ScrollDraw(0, -1);
  60.     }
  61. }
  62.  
  63. /* ScrollDown - control proc calls us
  64.  */
  65. static pascal void ScrollDown(ControlHandle c, Integer p){
  66.     if(inDownButton == p){
  67.         ScrollDraw(0, 1);
  68.     }
  69. }
  70.  
  71. /* PageHeight - return 9/10s of the window size
  72.  */
  73. static Integer PageHeight(void){
  74.     return    9L*(qd.thePort->portRect.bottom - qd.thePort->portRect.top - kScrollBarWidth) / 10L;
  75. }
  76.  
  77.  
  78. /* PageWidth - return 9/10s of the window size rounded down to the nearest
  79.    multiple of scale.
  80.  */
  81. static Integer PageWidth(void){
  82.     return    9L*(qd.thePort->portRect.right - qd.thePort->portRect.left - kScrollBarWidth) / 10L;
  83. }
  84.  
  85. /* PageUp -
  86.  */
  87. static pascal void PageUp(ControlHandle c, Integer p){
  88.     if(inPageUp == p){
  89.         ScrollDraw(0, -PageHeight());
  90.     }
  91. }
  92.  
  93. /* PageDown -
  94.  */
  95. static pascal void PageDown(ControlHandle c, Integer p){
  96.     if(inPageDown == p){
  97.         ScrollDraw(0, PageHeight());
  98.     }
  99. }
  100.  
  101. /* VThumb - thumb tracking. nothing special.
  102.  */
  103. static pascal void VThumb(){
  104. }
  105.  
  106. /* VDThumb called C style by ContentDoc
  107.    vertical done thumb
  108.  */
  109. static void VDThumb(ControlHandle c, Integer oldVal){
  110.     if(oldVal != GetCtlValue(c)){
  111.         DoScrollDraw(0, GetCtlValue(c) - oldVal);
  112.     }
  113. }
  114.  
  115. /* InitVThumb - set up for VDThumb
  116.  */
  117. static void InitVThumb(ControlHandle c){
  118. }
  119.  
  120. /******* horizontal scroll bar functions ******/
  121. /* these get executed repeatedly during the control activation */
  122.  
  123.  
  124. /* ScrollLeft - called from dispatch proc
  125.  */
  126. static pascal void ScrollLeft(ControlHandle c, Integer p){
  127.     if(inUpButton == p){
  128.         ScrollDraw(-1, 0);
  129.     }
  130. }
  131.  
  132. /* ScrollRight - called from dispatch proc
  133.  */
  134. static pascal void ScrollRight(ControlHandle c, Integer p){
  135.     if(inDownButton == p){
  136.         ScrollDraw(1, 0);
  137.     }
  138. }
  139.  
  140. /* PageLeft - called from dispatch proc
  141.  */
  142. static pascal void PageLeft(ControlHandle c, Integer p){
  143.     if(inPageUp == p){
  144.         ScrollDraw(-PageWidth(), 0);
  145.     }
  146. }
  147.  
  148. /* PageRight - called from dispatch proc
  149.  */
  150. static pascal void PageRight(ControlHandle c, Integer p){
  151.     if(inPageDown == p){
  152.         ScrollDraw(PageWidth(), 0);
  153.     }
  154. }
  155.  
  156. /* HThumb - no special thumb tracking
  157.  */
  158. static pascal void HThumb(){
  159. }
  160.  
  161. /* HDThumb called C style by ContentDoc
  162.     horizontal done thumb
  163.  */
  164. static void HDThumb(ControlHandle c, Integer oldVal){
  165.     if(oldVal != GetCtlValue(c)){
  166.         DoScrollDraw(GetCtlValue(c) - oldVal, 0);
  167.     }
  168. }
  169.  
  170. /* InitHThumb - set up for HDThumb
  171.  */
  172. static void InitHThumb(ControlHandle c){
  173. }
  174.  
  175. /* DoScrollDraw - actually scrolls the window.
  176.     doesn't set the controls. Use this when the user directly sets the control thumb.
  177.  
  178.     If we are going to scroll the window, we must empty the update event queue, since
  179.     the update region will be bogus after the scroll.
  180.  */
  181. static void DoScrollDraw(Integer x, Integer y){
  182.     RgnHandle    saveClip, scrollUpdate;
  183.     Rect        r;
  184.  
  185.     GetContentsRect(&r);
  186.     saveClip = NewRgn();
  187.     GetClip(saveClip);
  188.     scrollUpdate = NewRgn();
  189.     ScrollRect(&r, -x, -y, scrollUpdate);
  190.     SetClip(scrollUpdate);
  191.     RandomDotUpdate();
  192.     DisposeRgn(scrollUpdate);
  193.     SetClip(saveClip);
  194.     DisposeRgn(saveClip);
  195. }
  196.  
  197. /* TrimScroll - adjust number to preserve min < val < max relationship
  198.  */
  199. static Integer  TrimScroll(LongInt delta, ControlHandle c){
  200.     Integer newVal, oldVal, maxVal;
  201.  
  202.     oldVal = GetCtlValue(c);
  203.     maxVal = GetCtlMax(c);
  204.     newVal = oldVal + delta;
  205.     if(newVal > maxVal)
  206.         return maxVal - oldVal;
  207.     if(newVal < 0)
  208.         return  -oldVal;
  209.     return delta;
  210. }
  211.  
  212. /* ScrollDraw - scroll the window and change the controls.
  213.  */
  214. static void ScrollDraw(Integer x, Integer y){
  215.     ControlHandle        theH;
  216.     ControlHandle        theV;
  217.     
  218.     theH = ((RandomDotWindowPtr) qd.thePort)->hScroll;
  219.     x = TrimScroll(x, theH);
  220.  
  221.     theV = ((RandomDotWindowPtr) qd.thePort)->vScroll;
  222.     y = TrimScroll(y, theV);
  223.  
  224.     if(x == 0 && y == 0){
  225.         return;
  226.     }
  227.     if(x != 0){
  228.         SetCtlValue(theH, GetCtlValue(theH) + x);
  229.     }
  230.     if(y != 0){
  231.         SetCtlValue(theV, GetCtlValue(theV) + y);
  232.     }
  233.     DoScrollDraw(x, y);
  234. }
  235.